﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SysLogLook
{
    public partial class SysLog : Form
    {
        public SysLog()
        {
            InitializeComponent();
        }

        private void SysLog_Load(object sender, EventArgs e)
        {
            //装入系统日志
            button3_Click(null, null);
        }
        /// <summary>
        /// 显示系统日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //显示系统事件日志
            this.listView1.Items.Clear();
            //构造一个日志对象
            EventLog MySystemEvent = new EventLog();
            //说明这个日志是系统日志
            MySystemEvent.Log = "System";
            //获得日志的记录集合
            EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
            //记录的长度
            int Count = MyEventCollection.Count;
            this.groupBox1.Text = "显示系统日志(共" + Count.ToString() + ")个事件";
            for (int i = 0; i < Count; i++)
            {
                EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
                string[] SubItems ={ MyEntry.EntryType.ToString(),
                                    MyEntry.TimeGenerated.ToLongDateString(),
                                    MyEntry.TimeGenerated.ToLongTimeString(),
                                    MyEntry.Source,
                                    MyEntry.Category,
                                    MyEntry.EventID.ToString(),
                                    MyEntry.UserName,
                                    MyEntry.MachineName
                                  };
                ListViewItem MyItem = new ListViewItem(SubItems);
                this.listView1.Items.Add(MyItem);
            }
        }
        /// <summary>
        /// 显示安全性日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //显示安全性事件日志
            this.listView1.Items.Clear();
            //构造一个日志对象
            EventLog MySystemEvent = new EventLog();
            //说明这个日志是安全性日志
            MySystemEvent.Log = "Security";
            //获得日志的记录集合
            EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
            //记录的长度
            int Count = MyEventCollection.Count;
            this.groupBox1.Text = "显示安全性日志(共" + Count.ToString() + ")个事件";
            for (int i = 0; i < Count; i++)
            {
                EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
                string[] SubItems ={ MyEntry.EntryType.ToString(),
                                      MyEntry.TimeGenerated.ToLongDateString(),
                                      MyEntry.TimeGenerated.ToLongTimeString(),
                                      MyEntry.Source,
                                      MyEntry.Category,
                                      MyEntry.EventID.ToString(),
                                      MyEntry.UserName,
                                      MyEntry.MachineName
                                  };
                ListViewItem MyItem = new ListViewItem(SubItems);
                this.listView1.Items.Add(MyItem);
            }
        }
        /// <summary>
        /// 显示应用程序日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //显示应用程序事件日志
            this.listView1.Items.Clear();
            //构造一个日志对象
            EventLog MySystemEvent = new EventLog();
            //说明这个日志是应用程序日志
            MySystemEvent.Log = "Application";
            //获得日志的记录集合
            EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
            //记录的长度
            int Count = MyEventCollection.Count;
            this.groupBox1.Text = "显示应用程序日志(共" + Count.ToString() + ")个事件";
            for (int i = 0; i < Count; i++)
            {
                EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
                string[] SubItems ={ MyEntry.EntryType.ToString(),
                                      MyEntry.TimeGenerated.ToLongDateString(),
                                      MyEntry.TimeGenerated.ToLongTimeString(),
                                      MyEntry.Source,
                                      MyEntry.Category,
                                      MyEntry.EventID.ToString(),
                                      MyEntry.UserName,
                                      MyEntry.MachineName
                                  };
                ListViewItem MyItem = new ListViewItem(SubItems);
                this.listView1.Items.Add(MyItem);
            }
        }
    }
}
